# coding:utf-8
import requests
import time
import random
import pandas as pd
import re

headers = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip,deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.55'
}

urls = []
print('1')
for year in range(2015, 2020):
    for month in range(1, 12):
        urls.append(
            r'http://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D=57516&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'.format(
                year=year, month=month))

        # if year <= 2016:
        #     urls.append('http://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D=57516&areaInfo%5BareaType%5D=2&date%5Byear%5D=%s&date%5Bmonth%5D=%s' % (year, month))
        # else:
        #     if month < 10:
        #         urls.append('http://tianqi.2345.com/t/wea_history/js/%s0%s/58362_%s0%s.js' % (year, month, year, month))
        #     else:
        #         urls.append('http://tianqi.2345.com/t/wea_history/js/%s%s/58362_%s%s.js' % (year, month, year, month))
print(urls)
info = []
i = 1
for url in urls:
    seconds = random.randint(3, 4)
    response = requests.get(url, headers=headers).content
    response = response.decode('unicode_escape')

    # print(type(response))
    # response=response.decode('utf-8')
    # print(type(response))

    # print(response[:50])
    if i == 1:
        print(response)
        i += 1
    ymd = re.findall("""<tr>
        <td>(.*?)<\\\/td>""", response)  # 获取时间
    high = re.findall("""color:#ff5040;">(.*?)<\\\/td>""", response)  # 获取最高气温
    low = re.findall("""color:#3097fd;" >(.*?)<\\\/td>""", response)  # 获取最低气温

    info.append(pd.DataFrame({'日期：': ymd, '最高气温': high, '最低气温': low}))
    print("%s 数据已清洗完毕" % url)
    time.sleep(1)
weather = pd.concat(info)
print(info)
weather.to_csv('weather.csv', index=False)
print('完了')
